package com.ckx.base.support;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

import java.util.Map;

@Slf4j
public class MdcRunnable implements Runnable {

    private final Map<String, String> mdcMap;

    private final Runnable runnable;

    public MdcRunnable(Runnable runnable) {
        this.runnable = runnable;
        this.mdcMap = MDC.getCopyOfContextMap();
    }

    @Override
    public void run() {
        if (mdcMap != null) {
            mdcMap.forEach(MDC::put);
        }
        this.runnable.run();
        MDC.clear();
    }

}
